﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;
public class SignInModel : MonoBehaviour {
    /// <summary>
    /// 领取按钮
    /// </summary>
    public GameObject[] dayNumBtnsList;
    /// <summary>
    /// 关闭按钮
    /// </summary>
    public GameObject CloseBtn;
    /// <summary>
    /// 领取页面
    /// </summary>
    public GameObject SignInPanel;

    public int id;

    public List<SignInItem> itemlist;

    EginUser user = EginUser.Instance;
    void Awake () {
        for (int i = 0; i < dayNumBtnsList.Length;i++ )
        {
            UIEventListener.Get(dayNumBtnsList[i]).onClick = ButtonClick;

            itemlist.Add(dayNumBtnsList[i].GetComponent<SignInItem>());
        }
        UIEventListener.Get(CloseBtn).onClick = ButtonClick;
	}
    private void Start()
    {
        print(user.SignTimes);
        id = user.SignTimes - 1;
        GetTheSuccess();
    }

    void ButtonClick(GameObject button)
    {
        if (button.name == "CloseBtn")
        {
            Destroy(SignInPanel);
        }
        else
        {
            SignInItem signInItem = button.GetComponent<SignInItem>();

            id = signInItem.ID;
            if (signInItem)
            {
                SignRewardRequest lg = new SignRewardRequest { SignRewardTimes = id };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(lg, MsgId.SignRewardRequest);
            }
        }
    }

    /// <summary>
    /// 根据id 设定子物体的信息
    /// </summary>
    public void GetTheSuccess()
    {
        if (id == 7)
        {
            print(itemlist.Count);
            for (int i = 0; i < itemlist.Count; i++)
            {
                if (itemlist[i].ID == 1)
                {
                    itemlist[i].SetPlan(1);
                }
                else
                {
                    itemlist[i].SetPlan(0);
                }
            }
        }
        else
        {
            for (int i = 0; i < itemlist.Count; i++)
            {
                if (id == itemlist[i].ID)
                {
                    itemlist[i].SetPlan(2);
                }
                else if (itemlist[i].ID < id)
                {
                    itemlist[i].SetPlan(2);
                }
                else if (itemlist[i].ID == id + 1)
                {
                    itemlist[i].SetPlan(1);
                }
                else if (itemlist[i].ID > id + 1)
                {
                    itemlist[i].SetPlan(0);
                }
            }
        }
        
    }
	
}
